<script setup lang="ts">
import AlexBtn from '@2473o/alexui/packages/button/lib/button.vue'
import AlexDialog from '@2473o/alexui/packages/dialog/lib/dialog.vue'
import AlexSVG from '@2473o/alexui/packages/svg/lib/svg.vue'
import { ref, watch, toRefs, onMounted } from 'vue'

const show_dialog = ref(false)

const props = defineProps({
  images: {
    type: Array as () => string[],
    default: () => []
  },
  activeIndex: {
    type: Number,
    default: 0
  },
  Destory: {
    type: Function,
    default: () => {}
  }
})

onMounted(() => {
  show_dialog.value = true
})

const { images, activeIndex } = toRefs(props)
const show_fn = ref(true)
const LocalIndex = ref(activeIndex.value)

watch(
  () => LocalIndex.value,
  (newVal) => {
    if (newVal < 0) LocalIndex.value = images.value.length - 1
    else if (newVal >= images.value.length) LocalIndex.value = 0
  }
)

watch(
  () => show_dialog.value,
  (newVal) => {
    if (!newVal) props.Destory()
  }
)

const toggleFn = () => (show_fn.value = !show_fn.value)
</script>

<template>
  <AlexDialog v-model:show="show_dialog" :outside-close="true" top="50%" backdrop="rgba(48,55,66,0.3)">
    <template #header></template>
    <template #default>
      <div class="viewer">
        <ul class="views">
          <template v-for="(image, index) in images" :key="index">
            <li v-if="index === LocalIndex">
              <img @dblclick="toggleFn" :src="image" />
            </li>
          </template>
        </ul>
      </div>
      <div class="view-functions" v-show="show_fn">
        <div class="count">{{ LocalIndex + 1 }} / {{ images.length }}</div>
        <div class="switch">
          <AlexBtn height="2rem" width="2rem" radius=".2rem" @click="LocalIndex--"> ＜ </AlexBtn>
          <AlexBtn height="2rem" width="2rem" radius=".2rem" @click="LocalIndex++">
            <AlexSVG src="/assect/images/arrowright.svg" height="1rem"></AlexSVG>
          </AlexBtn>
        </div>
        <div class="close">
          <AlexBtn height="2rem" width="2rem" radius=".2rem" @click="show_dialog = false"> ✕ </AlexBtn>
        </div>
      </div>
    </template>
  </AlexDialog>
</template>

<style scoped lang="scss">
.alex-dialog {
  scrollbar-width: none;
  --content-bg: transparent;
  background-color: transparent;
  height: 100%;
  box-shadow: unset;
  border-radius: unset;
  :deep(.dialog_ctr) {
    padding: 0rem !important;
    max-width: 100%;
    height: 100%;
  }
  :deep(.dialog_content) {
    height: 100%;
  }
  &::backdrop {
    backdrop-filter: blur(8px);
  }
}

ul {
  list-style-type: none;
}

.viewer {
  width: 100%;
  height: 100%;
  min-width: 30rem;
  img {
    object-fit: contain;
    max-width: 100%;
    max-height: 100%;
    border-radius: 0.2rem;
    cursor: pointer;
  }
  ul.views {
    display: flex;
    height: 100%;
    li {
      margin-bottom: 1rem;
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

.view-functions {
  background-color: rgba(130, 136, 141, 0.75);
  color: #fff;
  bottom: 0;
  position: absolute;
  border-radius: 0.25rem;
  width: 100%;
  // max-width: 30rem;
  width: 30rem;
  padding: 0.55rem;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  left: 50%;
  transform: translateX(-50%);
  .alex-btn {
    color: #fff;
    background-color: rgba(255, 255, 255, 0.2);
    margin-top: 0.3rem;
    &:hover {
      background-color: rgba(255, 255, 255, 0.5);
    }
    & + button {
      margin-left: 0.5rem;
    }
  }
  .count {
    font-size: 0.95rem !important;
    margin-left: 0.5rem;
  }
  .switch {
    display: flex;
  }
}
</style>
